package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import easier.app.x.CommonCode;
import easier.app.x.result.ResultFragment;
import easier.app.x.result.ResultFragmentFactory;
import easier.log.logger.Logger;

public class MainActivity78 extends AppCompatActivity {

    private Button mClick;
    private Button mUseFragment;
    private Button mOpenNotify;
    private Button mBattery;
    private Button mAppSetting;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity78.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main78 );
        initView();
    }

    private void initView() {

        mClick = (Button) findViewById( R.id.click );
        mClick.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                MainActivity79.startActivityForResult( MainActivity78.this, 200 );
            }
        } );

        mUseFragment = (Button) findViewById( R.id.useFragment );
        mUseFragment.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                StartFragment fragment = new StartFragment();
                getSupportFragmentManager().beginTransaction().add( fragment, null ).commit();
            }
        } );

        mOpenNotify = (Button) findViewById( R.id.openNotify );
        mOpenNotify.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CommonCode.openNotificationSetting( MainActivity78.this, new ActivityResultCallback< Boolean >() {
                    @Override
                    public void onActivityResult( Boolean result ) {

                        CommonCode.showToast( v.getContext(), "通知是否打开:" + (result ? "打开" : "关闭") );
                    }
                } );
            }
        } );

        mBattery = (Button) findViewById( R.id.battery );
        mBattery.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( VERSION.SDK_INT >= VERSION_CODES.M ) {
                    ResultFragment< Void, Void > fragment = ResultFragmentFactory.buildBatteryOptimizations(
                        new ActivityResultCallback< Void >() {
                            @Override
                            public void onActivityResult( Void result ) {

                                Logger.i( "设置完毕" );
                            }
                        } );
                    getSupportFragmentManager().beginTransaction().add( fragment, null ).commit();
                }
            }
        } );

        mAppSetting = (Button) findViewById( R.id.appSetting );
        mAppSetting.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                ResultFragment< Void, Void > fragment = ResultFragmentFactory.buildAppSettings(
                    new ActivityResultCallback< Void >() {
                        @Override
                        public void onActivityResult( Void result ) {

                            CommonCode.showToast( v.getContext(), "app 设置了" );
                        }
                    } );
                getSupportFragmentManager().beginTransaction().add( fragment, null ).commit();
            }
        } );
    }

    @Override
    protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data ) {

        super.onActivityResult( requestCode, resultCode, data );

        Logger.i( "requestCode:" + requestCode + " resultCode:" + resultCode + " data:" + data );
    }

    public static class StartFragment extends Fragment {

        @Override
        public void onCreate( @Nullable Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );

            registerForActivityResult( new ActivityResultContract< Void, Void >() {
                @NonNull
                @Override
                public Intent createIntent( @NonNull Context context, Void input ) {

                    return new Intent( context, MainActivity79.class );
                }

                @Override
                public Void parseResult( int resultCode, @Nullable Intent intent ) {

                    return null;
                }
            }, new ActivityResultCallback< Void >() {
                @Override
                public void onActivityResult( Void result ) {

                    Logger.i( "读取到结果" );
                    getParentFragmentManager().beginTransaction().remove( StartFragment.this ).commit();
                }
            } ).launch( null );
        }
    }
}